/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ems.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.ems.entity.EmsClassroom;
import com.thinkgem.jeesite.modules.ems.entity.EmsDept;
import com.thinkgem.jeesite.modules.ems.entity.EmsMajor;
import com.thinkgem.jeesite.modules.ems.entity.EmsTeacher;
import com.thinkgem.jeesite.modules.ems.service.EmsDeptService;
import com.thinkgem.jeesite.modules.ems.service.EmsMajorService;
import com.thinkgem.jeesite.modules.ems.service.EmsTeacherService;

/**
 * 教师Controller
 * @author zad
 * @version 2018-01-25
 */
@Controller
@RequestMapping(value = "${adminPath}/ems/emsTeacher")
public class EmsTeacherController extends BaseController {

	@Autowired
	private EmsTeacherService emsTeacherService;
	
	@Autowired
	private EmsMajorService emsMajorService;
	
	@Autowired
	private EmsDeptService emsDeptService;
	
	@ModelAttribute
	public EmsTeacher get(@RequestParam(required=false) String id) {
		EmsTeacher entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = emsTeacherService.get(id);
		}
		if (entity == null){
			entity = new EmsTeacher();
		}
		return entity;
	}
	
	@RequiresPermissions("ems:emsTeacher:view")
	@RequestMapping(value = {"list", ""})
	public String list(EmsTeacher emsTeacher, HttpServletRequest request, HttpServletResponse response, Model model) {
		
		// add by zad 2018-01-25 专业查询
		List<EmsDept> depts = this.emsDeptService.findList(null);
		model.addAttribute("depts", depts);
		
		Page<EmsTeacher> page = emsTeacherService.findPage(new Page<EmsTeacher>(request, response), emsTeacher); 
		model.addAttribute("page", page);
		return "modules/ems/emsTeacherList";
	}
	
	@RequiresPermissions("ems:emsTeacher:view")
	@RequestMapping(value = {"select"})
	public String selectTeacher(String majorId,EmsTeacher emsTeacher, HttpServletRequest request, HttpServletResponse response, Model model) {
		
		EmsMajor major = emsMajorService.get(majorId);
		if(null == emsTeacher)
		{
			emsTeacher = new EmsTeacher();
		}
		emsTeacher.setDept(major.getDept());
		model.addAttribute("major", major);
		
		List<EmsDept> depts = this.emsDeptService.findList(major.getDept());
		model.addAttribute("depts", depts);
		
		Page<EmsTeacher> page = emsTeacherService.findPage(new Page<EmsTeacher>(request, response), emsTeacher); 
		model.addAttribute("page", page);
		return "modules/ems/emsTeacherSelect";
	}

	@RequiresPermissions("ems:emsTeacher:view")
	@RequestMapping(value = "form")
	public String form(EmsTeacher emsTeacher, Model model) {
		
		// add by zad 2018-01-25 专业查询
		List<EmsDept> depts = this.emsDeptService.findList(null);
		model.addAttribute("depts", depts);
		
		model.addAttribute("emsTeacher", emsTeacher);
		return "modules/ems/emsTeacherForm";
	}

	@RequiresPermissions("ems:emsTeacher:edit")
	@RequestMapping(value = "save")
	public String save(EmsTeacher emsTeacher, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, emsTeacher)){
			return form(emsTeacher, model);
		}
		emsTeacherService.save(emsTeacher);
		addMessage(redirectAttributes, "保存教师成功");
		return "redirect:"+Global.getAdminPath()+"/ems/emsTeacher/?repage";
	}
	
	@RequiresPermissions("ems:emsTeacher:edit")
	@RequestMapping(value = "delete")
	public String delete(EmsTeacher emsTeacher, RedirectAttributes redirectAttributes) {
		emsTeacherService.delete(emsTeacher);
		addMessage(redirectAttributes, "删除教师成功");
		return "redirect:"+Global.getAdminPath()+"/ems/emsTeacher/?repage";
	}

}